﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Express.Task;
using KZ.Utilities;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KZ.Express.Presenter
{
    public class LoginAccountDetailPresenter : PresenterBase
    {
        private new LoginAccountTask _task;

        public LoginAccountDetailPresenter(IViewBase view)
            : base(view, new UserENT())
        {
            _view = view;
            _task = new LoginAccountTask();
        }

        public virtual void Save()
        {
            try
            {
                if (_view.Action == ActionName.Insert)
                {
                    int entityID = _task.Insert(_view.Entity);
                    if (entityID > 0)
                    {
                        _view.Entity = null;
                        _view.SetMessage(Message.BLM_SUC_INSERT.ToString(), true);
                    }
                    else if (entityID == -1)
                        _view.SetMessage("BLM_ERR_USERNAME_EXISTS", false);
                }
                else if (_view.Action == ActionName.Update)
                {
                    int entityID = _task.Update(_view.Entity);
                    if (entityID > 0)
                    {
                        _view.SetMessage(Message.BLM_SUC_UPDATE.ToString(), true);
                    }
                    else if (entityID == -1)
                        _view.SetMessage("BLM_ERR_USERNAME_EXISTS", false);
                }
            }
            catch (DataAccessException ex)
            {
                throw new BusinessLogicException(ex.Message);
            }
        }
    }
}